package com.hp.bon.sgw.wh.mml.helper;

import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.mml.common.AckEntity;
import com.hp.bon.sgw.wh.mml.common.MMLSessionEntity;

public class HttpAckHelper {

	private static final Logger logger = LoggerFactory.getLogger(HttpAckHelper.class);

	public static void doHttpAck(MMLSessionEntity sessionEntity) {
		
		StringBuilder sbuf = new StringBuilder();
		sbuf.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		sbuf.append("<SOAP-ENV:Header/>");
		sbuf.append("<SOAP-ENV:Body>");
		sbuf.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
		
		sbuf.append("<MID>");
		sbuf.append(sessionEntity.getXmlMsg().getMID());
		sbuf.append("</MID>");
		sbuf.append("<REPTAG>");
		sbuf.append(sessionEntity.getXmlMsg().getREPTAG());
		sbuf.append("</REPTAG>");
		sbuf.append("<RTT>");
		sbuf.append(sessionEntity.getXmlMsg().getRTT());
		sbuf.append("</RTT>");
		sbuf.append("<SIG>");
		sbuf.append(sessionEntity.getXmlMsg().getSIG());
		sbuf.append("</SIG>");
		
		AckEntity ackEntity = sessionEntity.getAckEntity(AckEntity.TYPE_TRANSACTION);
		if(null==ackEntity){
			ackEntity = sessionEntity.getAckEntity(AckEntity.TYPE_LOGIN);
		}
		if(ackEntity!=null){
			sbuf.append("<RC>");
			sbuf.append(ackEntity.getAckItemValue(AckEntity.KEY_RETN));
			sbuf.append("</RC>");
			sbuf.append("<DS>");
			sbuf.append(ackEntity.getAckItemValue(AckEntity.KEY_DESC));
			sbuf.append("</DS>");
		} else if(sessionEntity.isTimeout()) {
			sbuf.append("<RC>");
			sbuf.append("-2");
			sbuf.append("</RC>");
			sbuf.append("<DS>");
			sbuf.append("Message Timeout");
			sbuf.append("</DS>");
		} else {
			sbuf.append("<RC>");
			sbuf.append("-1");
			sbuf.append("</RC>");
			sbuf.append("<DS>");
			sbuf.append("VC NOT RESPONSE");
			sbuf.append("</DS>");
		}
		
		sbuf.append("<DATA><![CDATA[");
		sbuf.append("<Service_Information>");
		if(ackEntity!=null){
			Map<String, String> items = ackEntity.getAckItems();
			Set<Entry<String, String>> entries = items.entrySet();
			for(Entry<String, String> entry : entries){
				sbuf.append("<");
				sbuf.append(entry.getKey());
				sbuf.append(">");
				sbuf.append(entry.getValue());
				sbuf.append("</");
				sbuf.append(entry.getKey());
				sbuf.append(">");
			}
		}
		sbuf.append("</Service_Information>");
		sbuf.append("]]></DATA>");
		sbuf.append("</ns3:SgwQueryAck>");
		sbuf.append("</SOAP-ENV:Body>");
		sbuf.append("</SOAP-ENV:Envelope>");
		
		String xml = sbuf.toString();
		logger.info("MML Session Id: " + sessionEntity.getSessionId());
		logger.info(xml);
		
		try {
			sessionEntity.getHttpResponse().write(xml.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sessionEntity.getHttpResponse().close();
		}
	}
	
	public static void errorAck(PrintStream bodyPrintStream, XmlMessage xmlMsg, String errorMsg) {
		
		StringBuilder sbuf = new StringBuilder();
		sbuf.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		sbuf.append("<SOAP-ENV:Header/>");
		sbuf.append("<SOAP-ENV:Body>");
		sbuf.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
		
		sbuf.append("<MID>");
		sbuf.append(xmlMsg.getMID());
		sbuf.append("</MID>");
		sbuf.append("<REPTAG>");
		sbuf.append(xmlMsg.getREPTAG());
		sbuf.append("</REPTAG>");
		sbuf.append("<RTT>");
		sbuf.append(xmlMsg.getRTT());
		sbuf.append("</RTT>");
		sbuf.append("<SIG>");
		sbuf.append(xmlMsg.getSIG());
		sbuf.append("</SIG>");
		sbuf.append("<RC>");
		sbuf.append("-1");
		sbuf.append("</RC>");
		sbuf.append("<DS>");
		sbuf.append(errorMsg);
		sbuf.append("</DS>");
		
		sbuf.append("</ns3:SgwQueryAck>");
		sbuf.append("</SOAP-ENV:Body>");
		sbuf.append("</SOAP-ENV:Envelope>");
		
		String xml = sbuf.toString();
		logger.info(xml);
		
		try {
			bodyPrintStream.write(xml.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			bodyPrintStream.close();
		}
	}
	
}
